<?php

namespace App\Http\Controllers\Supplier;
use App\Http\Controllers\Controller;
use App\Models\NewAdmin;
use App\Models\ShopUser;
use App\Models\ShopUserLog;
use App\Services\CommonService;
use App\Services\OtherCallbacksService;
use Illuminate\Http\Request;

//供应商入驻
class SupplierApplyController extends Controller
{
    //添加供应商
    public function addSupplier(Request $request)
    {
        $input = $request->input();
        if (empty($input['shopname']) || empty($input['photo']) || empty($input['shoptype']) || empty($input['people']) || empty($input['contact_tel'])
            || empty($input['faptype']) || empty($input['mechanism']) || empty($input['companyid']) || empty($input['company_area'] || empty($input['company']))
            || empty($input['shopcity']) || empty($input['shopcard']) || empty($input['doorphoto']) || empty($input['seal_url'] || empty($input['faname']))
            || empty($input['idcard']) || empty($input['faidcard']) || empty($input['faidcard_back']) || empty($input['area_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        if (!$this->limitChar($input['shopname'], 50)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺名称限制1-50个字符，中文为3个字符');
        }
        $shopUser = ShopUser::GetShopUser(array('shopname' => $input['shopname']));
        if (!empty($shopUser)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺名称重复,请重新输入');
        }
        if (!Verify_Phone($input['contact_tel'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
        } else {
            $shopUser = ShopUser::GetShopUser(array('contact_tel' => $input['contact_tel']));
            if ($shopUser) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该手机号已经申请过店铺');
            }
            $other_admin = NewAdmin::getOneNewAdmin(array('mobile' => $input['contact_tel']));
            if ($other_admin) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该手机号已占用,账号已存在');
            }
        }
        if (!isBusinessRegistrationNumber($input['companyid'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效工商注册号');
        }
        if (!check_passportid($input['idcard'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效法人身份证号');
        }
        $time = time();
        $addData = array(
            'shopname' => $input['shopname'], //店铺名称
            'photo' => $input['photo'], //店铺头像
            'shoptype' => $input['shoptype'], //商品类型
            'people' => $input['people'], //联系人
            'contact_tel' => $input['contact_tel'], //联系人手机号
            'mobile' => $input['contact_tel'], //联系人手机号
            'faptype' => $input['faptype'], //发票类型：1普通发票  2增值税发票  1,2普通发票+增值税发票
            'mechanism' => $input['mechanism'], //公司/机构名称
            'companyid' => $input['companyid'], //工商/营业执照注册号
            'company_area' => $input['company_area'], //公司所在区域 省-市-区
            'area_id' => $input['area_id'], //区域城市id
            'company' => $input['company'], //公司地址
            'shopcity' => $input['shopcity'], //当前经营位置 省-市
            'shopcard' => $input['shopcard'], //营业执照图片
            'doorphoto' => $input['doorphoto'], //公司门头照
            'seal_url' => $input['seal_url'], //公章图片
            'faname' => $input['faname'], //法人姓名
            'idcard' => $input['idcard'], //法人身份证号
            'faidcard' => $input['faidcard'], //法人身份证图片
            'faidcard_back' => $input['faidcard_back'], //法人身份证图片(带国徽的一面)
            'category_id' => $input['shoptype'], //申请品类父级id
            'member_id' => $time,
            'shop_balance' => 0,
            'frozen_amount' => 0,
            'last_login_ip' => getRealIp(),
            'addtime' => $time,
        );
        //有三方关联ID进行数据绑定
        if(isset($input['relationId']) && $input['relationId']){
            $addData['relation_id'] = $input['relationId'];
        }
        $res = ShopUser::addData($addData);
        if ($res) {
            //添加操作记录
            ShopUserLog::addLog(0, $time, 2, '供应商入驻', [], [], '申请入驻成为产品供应商');

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '提交成功，请等待平台审核');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败，请稍后再试或联系平台客服');
        }
    }

    //限制1-50个字符，中文为3个字符
    private function limitChar($str, $maxNum = 50)
    {
        $len = mb_strlen($str);
        if ($len >= 1 && $len <= $maxNum) {
            return true;
        }
        return false;
    }


}
